package leetcode;

import java.util.Stack;

public class Test0020 {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<Character>();
        if(s==null || s.length()<=0)
            return true;

        if(s.length()%2>0)
            return false;

        for(int i=0;i<s.length();i++){
            char c=s.charAt(i);
            if(c=='(' || c=='[' || c=='{'){
                stack.push(c);
            }
            else {
                if(stack.empty())
                    return false;
                if(c==')' && stack.pop()!='(')
                    return false;
                if(c==']' && stack.pop()!='[')
                    return false;
                if(c=='}' && stack.pop()!='{')
                    return false;
            }
        }
        if(stack.empty()==true)
            return true;
        else
            return false;
    }
}
